<template>
    <dzs-dialog :title="title" :isShow="isShow" @close="onCancel" @open="onOpen">
        <dzs-form :loadingOption="formInfo" :status="status" ref="myForm" v-model:value="formData"
            :options="formOptions" @onCancel="onCancel" @onSubmit="onSubmit">
        </dzs-form>
    </dzs-dialog>
</template>
<script setup lang="ts">
import { ref, computed } from "vue";
import type { dzsFormProps } from "@/components/dzs-form/@types";
import type { dzsFormInstance } from "@/components/dzs-form";
import type { formData } from "../@types";
import { ElNotification } from 'element-plus';
import { addMyAsset, updateMyAsset } from "../api/api";
import type { tableItem } from "../@types";
const emit = defineEmits(['cancel', 'update:isShow', 'onHoldSearch'])
const props = defineProps<{
    dialogData?: tableItem,
    status: string,
    isShow: boolean
}>()

const myForm = ref<dzsFormInstance>();
const formData = ref<formData>({
    ID: "",
    title: "",
    buy_time: "",
    buy_amount: "",
    remarks: "",
})

const formInfo = ref({
    loading: false,
})

const title = computed(() => {
    return props.status === "add" ? "添加我的资产" : props.status === "edit" ? "编辑我的资产" : "查看我的资产详情"
})
const formOptions = computed<dzsFormProps['formOptions']>(() => {
    return {
        formProps: {
            labelWidth: "120px"
        },
        formItem: [
            {
                label: "资产名称",
                type: "input",
                key: "title",
                rules : [
                    { required: true, message: "请输入资产名称", trigger: "blur" }
                ]
            },
            {
                label: "购买时间",
                type: "date",
                props: {
                    format: 'YYYY-MM-DD',
                    "value-format": "YYYY-MM-DD"
                },
                key: "buy_time",
                rules : [
                    { required: true, message: "请选择购买时间", trigger: "change" }
                ]
            },
            {
                label: "购买金额",
                type: "input",
                key: "buy_amount",
                props : {
                    type : "number"
                },
                rules : [
                    { required: true, message: "请输入购买金额", trigger: "blur" }
                ]
            },
            {
                label: "备注",
                type: "input",
                key: "remarks",
                props : {
                    type : "textarea",
                    rows : 3
                }
            }
        ]
    }
})

const onCancel = () => {
    myForm.value?.onCancel<formData>(false, {
        success: (res) => {
            formData.value = res
        }
    })
    emit('cancel');
    emit('update:isShow', false);
}


const onOpen = () => {
    if (props.dialogData?.ID) {
        formData.value = props.dialogData
    }
}

const onSubmit = () => {
    formInfo.value.loading = true;
    if (props.status === "edit") {
        updateMyAsset({
            ...formData.value
        }).then(res => {
            formInfo.value.loading = false;
            ElNotification({
                title: '成功',
                message: "提交成功",
                type: 'success'
            })
            emit("onHoldSearch");
            onCancel();
        }).catch(() => {
            formInfo.value.loading = false;
        })
        return;
    }
    addMyAsset({
        ...formData.value
    }).then(res => {
        formInfo.value.loading = false;
        ElNotification({
            title: '成功',
            message: "提交成功",
            type: 'success'
        })
        onCancel();
        emit("onHoldSearch");
    }).catch(() => {
        formInfo.value.loading = false;
    })
}
</script>
<style scoped></style>